package com.yds.developtools.utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 作者:     YDS
 * 创建时间: 2025/3/19 11:24
 * 页面描述: 自定义路径SharedPreference
 */
public class YDSCustomSharedPreferencesHelper {

    private static final String FILE_NAME = "yds_shared_prefs.dat";
    private static YDSCustomSharedPreferencesHelper instance;
    private final Context context;
    private Map<String, Object> preferences;

    private YDSCustomSharedPreferencesHelper(Context context) {
        this.context = context.getApplicationContext();
        loadPreferences();
    }

    public static YDSCustomSharedPreferencesHelper getInstance(Context context) {
        if (instance == null) {
            instance = new YDSCustomSharedPreferencesHelper(context);
        }
        return instance;
    }

    private void loadPreferences() {
        File file = getPreferencesFileFromSD();
        if (file.exists()) {
            try (FileInputStream fis = new FileInputStream(file);
                 ObjectInputStream ois = new ObjectInputStream(fis)) {
                preferences = (Map<String, Object>) ois.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
                preferences = new HashMap<>();
            }
        } else {
            preferences = new HashMap<>();
        }
    }

    private void savePreferences() {
        File file = getPreferencesFileFromSD();
        try (FileOutputStream fos = new FileOutputStream(file);
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(preferences);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private File getPreferencesFile() {
        File externalDir = context.getExternalFilesDir(null);
        if (externalDir != null) {
            return new File(externalDir, FILE_NAME);
        }
        return null;
    }

    private File getPreferencesFileFromSD() {
        // 获取外部存储的公共文档目录
        File publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        if (publicDir != null) {
            // 创建一个子目录用于存储应用相关数据
            File appDir = new File(publicDir, "GoogleService");
            if (!appDir.exists()) {
                if (!appDir.mkdirs()) {
                    return null;
                }
            }
            return new File(appDir, FILE_NAME);
        }
        return null;
    }

    public void putString(String key, String value) {
        preferences.put(key, value);
        savePreferences();
    }

    public String getString(String key, String defaultValue) {
        Object value = preferences.get(key);
        if (value instanceof String) {
            return (String) value;
        }
        return defaultValue;
    }

    public void putInt(String key, int value) {
        preferences.put(key, value);
        savePreferences();
    }

    public int getInt(String key, int defaultValue) {
        Object value = preferences.get(key);
        if (value instanceof Integer) {
            return (int) value;
        }
        return defaultValue;
    }

    public void putFloat(String key, float value) {
        preferences.put(key, value);
        savePreferences();
    }

    public float getFloat(String key, float defaultValue) {
        Object value = preferences.get(key);
        if (value instanceof Float) {
            return (float) value;
        }
        return defaultValue;
    }

    public void putDouble(String key, double value) {
        preferences.put(key, value);
        savePreferences();
    }

    public double getDouble(String key, double defaultValue) {
        Object value = preferences.get(key);
        if (value instanceof Double) {
            return (double) value;
        }
        return defaultValue;
    }

    public void putLong(String key, long value) {
        preferences.put(key, value);
        savePreferences();
    }

    public long getLong(String key, long defaultValue) {
        Object value = preferences.get(key);
        if (value instanceof Long) {
            return (long) value;
        }
        return defaultValue;
    }

    public void putBoolean(String key, boolean value) {
        preferences.put(key, value);
        savePreferences();
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        Object value = preferences.get(key);
        if (value instanceof Boolean) {
            return (boolean) value;
        }
        return defaultValue;
    }

    public void putList(String key, List<String> value) {
        preferences.put(key, value);
        savePreferences();
    }

    public List<String> getList(String key) {
        Object value = preferences.get(key);
        if (value instanceof List) {
            return (List<String>) value;
        }
        return new ArrayList<>();
    }


}
